import { SportMenuResp } from "../response/SportMenuResp"
import * as _ from "lodash"
import { validSportMenu, validSportMenuID } from "../ValidSportMenu"
import { plainToClass, Type } from "class-transformer"

export class Odd {
  key: string
  title: string
}

/**
 * Ui 数据
 */
export class SportModel {
  title: string
  id: number
  count: number
  @Type(() => Odd)
  play: Odd[]
}

/**
 * getmenu UI映射
 */
export class SportMenuMap {
  sportMenu: SportMenuResp

  constructor(sportMenu: SportMenuResp) {
    this.sportMenu = sportMenu
  }

  public getListByType(pt: number) {
    switch (pt) {
      case 1:
      case 10: {
        return this.sportMenu.tm
      }
      case 2: {
        return this.sportMenu.em
      }
      case 3: {
        return this.sportMenu.pm
      }
      case 4: {
        return this.sportMenu.im
      }
    }
    return null
  }

  /**
   * 获取几大类型赛事数量
   * @param pt 今日：1，早盘：2，串关：3，滚球：4，收藏：5，串关中的今日：7
   */
  public getTypeCount(pt: number): number {
    let result = 0

    let type = this.getListByType(pt)

    if (!type) {
      return result
    }

    for (let i = 0; i < type.length; i++) {
      if (validSportMenuID.indexOf(type[i].k) === -1) {
        continue
      }
      if (pt === 4) {
        result = result + type[i].ec
      } else {
        if (!_.isEmpty(type[i].ml)) {
          type[i].ml.forEach(function(item) {
            if (item.bt === "am") {
              result = result + item.ec
            }
          })
        }
      }
    }

    // 今日包含滚球数据，但是列表不显示，所以这里要去掉
    if (pt === 1) {
      result = result - this.getTypeCount(4)
    }

    return result
  }

  /**
   * 获取对应运动的比赛的数量
   * @param pt
   * @param sportId
   */
  public getSportCount(pt: number, sportId: number): number {
    let count = 0

    if (validSportMenuID.indexOf(sportId) === -1) {
      return count
    }
    let typeList = this.getListByType(pt)
    if (_.isEmpty(typeList)) {
      return count
    }
    typeList.forEach(function(item) {
      if (pt === 4) {
        if (item.k === sportId) {
          count = count + item.ec
        }
      } else {
        if (!_.isEmpty(item.ml) && item.k === sportId) {
          item.ml.forEach(function(itemMl) {
            if (itemMl.bt === "am") {
              count = count + itemMl.ec
            }
          })
        }
      }
    })

    // 今日包含滚球数据，但是列表不显示，所以这里要去掉
    if (pt === 1) {
      count = count - this.getSportCount(4, sportId)
    }
    return count
  }

  /**
   * 获取所有比赛的数量
   * @param pt 今日：1，早盘：2，串关：3，滚球：4，收藏：5，串关中的今日：7
   */
  public getAllSportCount(pt: number): Map<number, SportModel> {
    const sportModes = plainToClass(SportModel, validSportMenu)

    let mapSport = new Map<number, SportModel>()

    for (let i = 0; i < sportModes.length; i++) {
      sportModes[i].count = this.getSportCount(pt, sportModes[i].id)
      if (pt === 4) {
        // TODO 即将开赛数据
      }
      mapSport.set(sportModes[i].id, sportModes[i])
    }
    return mapSport
  }
}
